package com.spic.business.train.questions.controller;

import com.spic.business.train.questions.domain.TrainQuestionVo;
import com.spic.business.train.questions.domain.TrainQuestions;
import com.spic.business.train.questions.service.ITrainQuestionsService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 题库管理Controller
 *
 * @author renxiang
 * @date 2021-10-20
 */
@Slf4j
@RestController
@RequestMapping("/train/questions")
public class TrainQuestionsController extends BaseController
{
    @Autowired
    private ITrainQuestionsService trainQuestionsService;

    /**
     * 查询题库管理列表
     */
//    @PreAuthorize(hasPermi = "business/train:questions:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainQuestions trainQuestions)
    {
        startPage();
        List<TrainQuestions> list = trainQuestionsService.selectTrainQuestionsList(trainQuestions);
        return getDataTable(list);
    }

    /**
     * 导出题库管理列表
     */
//    @PreAuthorize(hasPermi = "business/train:questions:export")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainQuestions trainQuestions) throws IOException
    {
        List<TrainQuestions> list = trainQuestionsService.selectTrainQuestionsList(trainQuestions);
        ExcelUtil<TrainQuestions> util = new ExcelUtil<TrainQuestions>(TrainQuestions.class);
        util.exportExcel(response, list, "题库管理数据");
    }

    /**
     * 获取题库管理详细信息
     */
//    @PreAuthorize(hasPermi = "business/train:questions:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(trainQuestionsService.selectTrainQuestionsById(id));
    }

    /**
     * 新增题库管理
     */
//    @PreAuthorize(hasPermi = "business/train:questions:add")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainQuestions trainQuestions)
    {
        return toAjax(trainQuestionsService.insertTrainQuestions(trainQuestions));
    }

    /**
     * 修改题库管理
     */
//    @PreAuthorize(hasPermi = "business/train:questions:edit")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainQuestions trainQuestions)
    {
        return toAjax(trainQuestionsService.updateTrainQuestions(trainQuestions));
    }

    /**
     * 删除题库管理
     */
//    @PreAuthorize(hasPermi = "business/train:questions:remove")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(trainQuestionsService.deleteTrainQuestionsByIds(ids));
    }

    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestPart(value = "file", required = true) MultipartFile file,TrainQuestions trainQuestions) throws Exception
    {
        ExcelUtil<TrainQuestionVo> util = new ExcelUtil<TrainQuestionVo>(TrainQuestionVo.class);
        List<TrainQuestionVo> trainQuestionVos = util.importExcel(file.getInputStream());
        String message = trainQuestionsService.importData(trainQuestionVos,trainQuestions);
        return AjaxResult.success(message);
    }

    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        try(ServletOutputStream servletOutputStream = response.getOutputStream()){
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("试题导入模板.xls","utf8"));
            ClassPathResource classPathResource = new ClassPathResource("Excel_model/选择题导入模板.xls");
            InputStream inputStream = classPathResource.getInputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        }catch (IOException e){
            log.info(e.getMessage());
        }catch (RuntimeException e) {
            log.info(e.getMessage());
        }
    }
}
